﻿using System;
using System.Windows.Forms;


namespace Ledict.Utility.Frm
{
    /// <summary>
    /// 错误报送窗体，捕获系统所有异常，进行统一处理。
    /// </summary>
    public partial class FrmBugReport : Form
    {
        public FrmBugReport()
        {
            InitializeComponent();
            //this.ShowIcon = false;//默认不显示图标
        }

        #region 全局变量
        Exception _bugInfo;
        #endregion

        #region 构造函数
        /// <summary>
        /// Bug发送窗口
        /// </summary>
        /// <param name="bugInfo">Bug信息</param>
        public FrmBugReport(Exception bugInfo)
        {
            InitializeComponent();
            _bugInfo = bugInfo;
            this.txtBugInfo.Text = bugInfo.Message;
            txtBugStackTrace.Text = bugInfo.StackTrace;
            lblErrorCode.Text = Guid.NewGuid().ToString();
        }

        /// <summary>
        /// Bug发送窗口
        /// </summary>
        /// <param name="bugInfo">Bug信息</param>
        /// <param name="errorCode">错误号</param>
        public FrmBugReport(Exception bugInfo, string errorCode)
        {
            InitializeComponent();
            _bugInfo = bugInfo;
            txtBugInfo.Text = bugInfo.Message;
            txtBugStackTrace.Text = bugInfo.StackTrace;
            lblErrorCode.Text = errorCode;

            //记录日志
            //LogFactory.GetLogger("message").Error(txtBugInfo.Text, _bugInfo);
            //LogFactory.GetLogger("message").Info(txtBugInfo.Text);
        }
        #endregion

        #region 公开静态方法
        /// <summary>
        /// 提示Bug
        /// </summary>
        /// <param name="bugInfo">Bug信息</param>
        /// <param name="errorCode">错误号</param>
        public static void ShowBug(Exception bugInfo, string errorCode)
        {
            new FrmBugReport(bugInfo, errorCode).ShowDialog();
        }

        /// <summary>
        /// 提示Bug
        /// </summary>
        /// <param name="bugInfo">Bug信息</param>
        public static void ShowBug(Exception bugInfo)
        {
            ShowBug(bugInfo, Guid.NewGuid().ToString());
        }
        #endregion

        private void btnDetailsInfo_Click(object sender, EventArgs e)
        {
            MessageBox.Show(@"异常详细信息：" + _bugInfo.Message + @"\r\n跟踪：" + _bugInfo.StackTrace);
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            Close();
            

            //重启
            if (chkReset.Checked)
            {
                //LogFactory.GetLogger().Info("系统重启");
                Application.Restart();
            }

        }

        /// <summary>
        /// 查看错误详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMsgDetail_Click(object sender, EventArgs e)
        {
            Height = 626;//form变高
            txtBugStackTrace.Visible = true;
            btnBack.Visible = true;
        }

        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            Height = 450;//form隐藏部分
            txtBugStackTrace.Visible = false;
            btnBack.Visible = false;
        }
    }
}
